/*****************************************************************************/
/*                                                                           */
/*                    TW8823 Demo Board   Techwell,Inc                       */
/*                                                                           */
/* CPU        : DP8051                                                       */
/* LANGUAGE   : Keil C                                                       */
/* PROGRAMMER : Harry Han                                                    */
/*                                                                           */
/*****************************************************************************/
/* See 'Release.txt' for firmware revision history                           */

#include "Config.h"
#include "reg.h"
#include "typedefs.h"
#include "Printf.h"
#include "Monitor.h"
#include "I2C.h"
#include "CPU.h"
#include "SPI.h"
#include "NVMData.h"
#include "Global.h"
#include "OSD\app_OSD.h"
#include "OSD\User_handle.h"
#include "app_MSG.h"
#include "app_SRC.h"
#include "app_GUI.h"

#ifdef TW8823EVB
#define VERSTR "EVB"
#endif

#ifdef TW8823SHDEMO
#ifdef TW8823INNOLUX
#define VERSTR "SHDEMO(I)"
#endif
#ifdef TW8823HANSTAR
#define VERSTR "SHDEMO(H)"
#endif
#endif

BYTE GUITimerID = 0;
BYTE SRCTimerID = 0;
BYTE PageREG;

void InitSystem(void)
{
	//----- Set SPI mode
	SPI_Init();

	#ifdef address_32bit //erek for 4byte address spi, 20121214
	SPI_Addr32Enable();
	#endif

	//----- Enable MCU Busy Check 0x403[6]=1
	WriteTW88( 0xff, OSD_PAGE );
	WriteTW88( 0x03, ReadTW88( 0x03 ) | 0x40 );

	//----- ?????
	WriteTW88( 0xff, MCU_PAGE );
	WriteTW88( 0x28, 0 );
	WriteTW88( 0x29, 12 );

	return;
}

//=============================================================================
//	SelfTest(void)	                                               
//=============================================================================
//void SelfTest(void)
//{
	//unsigned short int x=1;
	//unsigned char *p=0;
	//unsigned long temp =0;
	//p=(unsigned char*)(&x);
	//Printf("Is Little Endian ---- %bd\r\n",*p);
	//Printf("sizeof unsigned long = %bd Bytes.\r\n",sizeof(unsigned long));
	//Printf("sizeof unsigned int = %bd Bytes.\r\n",sizeof(unsigned int));
	//Printf("sizeof unsigned short int = %bd Bytes.\r\n",sizeof(unsigned short int));	
	//WriteI2C(0x88, 0xff, 0x00);
	//Printf("I2C Get TW8823 ID = 0x%bx\r\n",(unsigned char)ReadI2C(0x88,0x00));
	//WriteTW88(0xff, 0x00);
	//Printf("RAM Get TW8823 ID = 0x%bx\r\n",(unsigned char)ReadTW88(0x00));
	//Printf("I2C Get TW9900 ID = 0x%bx\r\n",(unsigned char)ReadI2C(0x8a,0x00));
//}

//=============================================================================
//	main(void)			                                               
//=============================================================================
void main(void)
{
	BYTE  tmp;
	//XDATA BYTE Test[6];
	
	InitCPU();
	Printf("\n\n\n\n\n\n\n\n\n\n");
	Printf("\n********************************************************");
	Printf("\n    TW8823 (DP8051) %s - %s (%s)", VERSTR, __TIME__, __DATE__);
	Printf("\n********************************************************");
	Printf("\nInitialize...");
	InitSystem();
	WriteTW88(0xff, MCU_PAGE);
	WriteTW88(0x20, (ReadTW88(0x20) | 0x40));	  // reset register
	delay(1);
	SetMonAddress(TW88I2CAddress);  //erek 2010-7-19
	SPI_QUADInit();
	SPI_TurboInit();
	InitNVMData();
	Init_SRCAPP();

	osd_animation(SPLASH_SCREEN_INDEX, SPLASH_SCREEN_LENGTH); //eamon.fang show splash screen
	
	Init_GUIAPP();
	Init_MSGAPP();
	Printf("\nComplete");

	//SPI_ReadData2xdata(0x100030,Test,6);

	//Printf("\r\n Test[0]=%bx, Test[1]=%bx,Test[2]=%bx, Test[3]=%bx,Test[4]=%bx, Test[5]=%bx\r\n",Test[0],Test[1],Test[2],Test[3],Test[4],Test[5]);
	
	GUITimerID = AddTimer0_Dly(ZGetSystemTimeLooper());
	SRCTimerID = AddTimer0_Dly(2);
//	EnableWatchdog();

	Prompt(); //erek 2010-7-19
	PageREG = ReadTW88(0xff);
	
	//void PWMinit(void)
	//{  
	//WriteTW88( 0xff, 0x09 );//#define PWM_PAGE 0x09
	//WriteTW88( 0x87, 0x20 );//PWM Control Register
	//WriteTW88( 0x88, 0xc8 );//PWM Control Register
	//WriteTW88( 0x89, 0x01 );//PWM clock Diveder Register
	//WriteTW88( 0x8a, 0xe2 );//PWM clock Diveder Register
	//}
	//WriteTW88(0xff,0x0f); //test Uart0 erek 2011-11-7
	//WriteTW88(0x20,ReadTW88(0x20)&0xdf);
	
	while (1)
	{
/*
		//-------------- Check Watchdog -------------------------
		if (F_watch)
		{
			F_watch = 0;
			Printf("\nWatchdog Interrupt !!! %02bx", WDCON);
		}
		EnableWatchdog();
*/

		//-------------- Check Serial Port ----------------------
		WriteTW88(0xff, PageREG);
		Monitor();	//erek 2010-7-19			// for new monitor functions
		PageREG = ReadTW88(0xff);
		//-------------- Send TSC/KEY/IR Message ----------------
		TaskMSG();

		//-------------- Check GUI Message ----------------------
		TaskGUI();
		
		//-------------- Timer ----------------------------------
		if (WaitTimer0(GUITimerID))
		{
//			SwapLED114;
			ZUITimerRun();
			RstTimer0_Dly(GUITimerID, ZGetSystemTimeLooper());
		}
		
		//-------------- Timer Det Source -----------------------
		if (WaitTimer0(SRCTimerID))
		{
//			SwapLED115;
//			TaskSRC();
			RstTimer0_Dly(SRCTimerID, 40);
		}
 	}

	return;
}

